package com.google.client;


import java.util.List;

import com.google.client.mvp.AppActivityMapper;
import com.google.client.mvp.AppPlaceHistoryMapper;
import com.google.client.ClientFactory;
import com.google.client.place.SamplePlace;

import com.google.gwt.activity.shared.ActivityManager;
import com.google.gwt.activity.shared.ActivityMapper;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.place.shared.Place;
import com.google.gwt.place.shared.PlaceController;
import com.google.gwt.place.shared.PlaceHistoryHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.FlexTable;

public class MarketManager implements EntryPoint {
  
	private SimplePanel appWidget = new SimplePanel();
	private Place defaultPlace = new SamplePlace("Go!");
	private MarketingServiceAsync marketingSvc = GWT.create(MarketingService.class);
	private FlexTable flexTable;

	public void onModuleLoad() {
		// Create ClientFactory using deferred binding so we can replace with 
		// different impls in gwt.xml
		ClientFactory clientFactory = GWT.create(ClientFactory.class);
		EventBus eventBus = clientFactory.getEventBus();
		PlaceController placeController = clientFactory.getPlaceController();
		// Start ActivityManager for the main widget with our ActivityMapper
		ActivityMapper activityMapper = new AppActivityMapper(clientFactory);
		ActivityManager activityManager = new ActivityManager(activityMapper, eventBus);
		activityManager.setDisplay(appWidget);
		// Start PlaceHistoryHandler with our PlaceHistoryMapper
		AppPlaceHistoryMapper historyMapper = GWT .create(AppPlaceHistoryMapper.class);
		PlaceHistoryHandler historyHandler = new PlaceHistoryHandler(historyMapper);
		historyHandler.register(placeController, eventBus, defaultPlace);
		RootPanel rootPanel = RootPanel.get();
		
		AddProduct product_ = new AddProduct();
		rootPanel.add(product_);
		//product_.setSize("100%", "100%");
		
		rootPanel.add(appWidget);
		
		flexTable = new FlexTable();
		flexTable.setText(0, 0, "name");	
		getList();
		rootPanel.add(flexTable);
		//flexTable.setSize("100%", "100%");
		
		//ProductList productList = new ProductList();
		//rootPanel.add(productList, 7, 220);
		
		
		// Goes to place represented on URL or default place
		historyHandler.handleCurrentHistory();
	}
	private void getList() 
	{
			// Initialize the service proxy.
			    if (marketingSvc == null) {
			    	marketingSvc = GWT.create(MarketingService.class);
			    }

			    // Set up the callback object.
			    AsyncCallback<List<Product>> callback = new AsyncCallback<List<Product>>() {
			      public void onFailure(Throwable caught) {
			  		Window.alert(caught.getMessage());

			         }

			      public void onSuccess(List<Product> result) {
			    	  //rows = result;
			    	  int i=1;
			    	  for (Product p : result) {
			    		  flexTable.setText(i, 0, p.getName());	
			    		  i++;}
			      }
			    };

			    // Make the call to the stock price service.
			    marketingSvc.getProductList(callback);
		}
}
